<template>
	<!-- weixinmao_hssy/pages/user/index.wxml -->
	<view class="container">
		<view class="header">
			<view v-if="isuser">
				<view class="pic"></view>
			</view>
		</view>

		<view class="mymoney textCen">
			<view>账户余额(元)</view>
			<view>￥{{ totalmoney }}</view>
			<view @tap="toGetmoney">我要提现</view>
			<view class="mar-top15" @tap="cz">充值</view>
		</view>

		<view class="list">
			<view class="item" @tap="toMymoneyrecord">
				<image src="/static/imgs/icon/05.png"></image>
				<view style="margin-left: 16rpx">变动记录</view>
			</view>
			<view class="item" @tap="toWithdraw">
				<image src="/static/imgs/icon/03.png"></image>
				<view style="margin-left: 16rpx">提现记录</view>
			</view>
		</view>
	</view>
</template>

<script>
	// pages/user/index.js
	import {
		Token
	} from '../../utils/token.js';
	import {
		My
	} from '../my/my-model.js';
	var jweixin = require('jweixin-module');
	import {
		Goods
	} from '../../model/integralgoods-model';
	var Goods2 = new Goods();
	var my = new My();
	export default {
		data() {
			return {
				isuser: true,
				tel: '',
				usermoney: 0,
				totalmoney: '',
				userinfo: '',
				intervalId: null
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			this.onLoadClone3389(options);
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			var that = this;
			
			 this.intervalId = setInterval(()=>{
				console.log(123)
				that.onLoadClone3389({})
			 }, 3000);
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {
			clearInterval(this.intervalId);
		},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {
			clearInterval(this.intervalId);
		},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			uni.showNavigationBarLoading();
			this.onLoadClone3389({});
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {
			var that = this;
			return {
				title: '会员中心',
				path: '/pages/user/index'
			};
		},
		methods: {
			/* 
			 充值
			 */
			cz() {
				uni.navigateTo({
					url: '/pages/mymoney/recharge'
				})
			},
			/**
			 * 生命周期函数--监听页面加载
			 */
			onLoadClone3389: function(options) {
				console.log('执行我了')
				var that = this;
				uni.setNavigationBarTitle({
					title: '我的钱包'
				});
				var appuser = uni.getStorageSync('userInfo');
				var params = {};
				my.UserInit((data) => {
					that.usermoney = data.usermoney;
					that.setData({
						totalmoney: data.totalmoney
					});
					uni.hideNavigationBarLoading(); //完成停止加载
					uni.stopPullDownRefresh();
				}, params);
				that.setData({
					userinfo: appuser
				});
			},

			toGetmoney: function() {
				var that = this;
				if (that.usermoney <= 0) {
					uni.showModal({
						title: '提示',
						content: '余额不足,无法提现',
						showCancel: false
					});
					return;
				} else {
					uni.showModal({
						title: '提示',
						content: '确认提现？',
						success: function(res) {
							if (res.confirm) {
								uni.showLoading({
									title: '加载中...'
								})
								var params = {};
								my.getUserMoney((data) => {
									console.log(data)
									uni.hideLoading()
									if (data.code == 0) {
										that.dakuan(data)
									} else {
										uni.showModal({
											title: '提示',
											content: data.msg,
											showCancel: false
										});
										return;
									}
								}, params);
							}
						}
					});
				}
			},
			dakuan(datas) {
				console.log('调用打款了')
				var that = this
				var surl = encodeURIComponent(window.location.href.split('#')[0]); //据说可以解决URL中带参数的问题，前台用的js编码，后台php解码
				console.log(surl)
				console.log(window.location.href)
				let params = {
					weburl: encodeURIComponent(window.location.href)
				}
				Goods2.jsapiParams((data) => {
					console.log(data)
					if (data.code === 1) {
						jweixin.config({
							debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。  
							appId: data.data.appid, // 必填，公众号的唯一标识  
							timestamp: data.data.timestamp, // 必填，生成签名的时间戳  
							nonceStr: data.data.noncestr, // 必填，生成签名的随机串  
							signature: data.data.signature, // 必填，签名，见附录1  
							surl: data.data.url, //自己添加的，debug为true的时候可以网页打印出对应的URL是否正确  
							jsApiList: ["requestMerchantTransfer"]
						});
						jweixin.checkJsApi({
							jsApiList: ['requestMerchantTransfer'],
							success: function(res) {
								if (res.checkResult['requestMerchantTransfer']) {
									WeixinJSBridge.invoke('requestMerchantTransfer', {
											mchId: datas.data.mch_id,
											appId: datas.data.appid,
											package: datas.data.package,
										},
										function(res) {
											if (res.err_msg === 'requestMerchantTransfer:ok') {
												// res.err_msg将在页面展示成功后返回应用时返回success，并不代表付款成功
											}
										}
									);
								} else {
									alert('你的微信版本过低，请更新至最新版本。');
								}
							}
						});
					} else {
						uni.showToast({
							title: data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				}, params)

			},

			toMymoneyrecord: function() {
				var that = this;
				uni.navigateTo({
					url: '/pages/mymoneyrecord/index'
				});
			},
			toWithdraw() {
				uni.navigateTo({
					url: '/pages/mymoney/withdraw'
				});
			},
		}
	};
</script>
<style>
	page {
		background: #f5f5f5;
	}

	.container {
		background-color: #f5f5f5;
		min-height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.container .header {
		width: 100%;
		height: 330rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		background-image: url('https://api.site100.cn/userbg.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;
	}

	.container .header .pic {
		width: 100%;
		height: 250rpx;
		align-self: center;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding-left: 20rpx;
	}

	.container .header .pic image {
		width: 120rpx;
		height: 120rpx;
		border: 0rpx solid #fff;
		border-radius: 120rpx;
	}

	.container .header .pic .userinfo {
		height: 110rpx;
		width: 580rpx;
		font-size: 0.8rem;
		margin-left: 15rpx;
		color: #fff;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.mymoney {
		width: 700rpx;
		padding-top: 20rpx;
		background: #fff;
		border-radius: 25rpx;
		margin-top: -220rpx;
		height: 440rpx;
	}

	.mymoney view:nth-child(1) {
		margin-top: 50rpx;
		color: #a2a2a2;
	}

	.mymoney view:nth-child(2) {
		margin-top: 40rpx;
		font-size: 22px;
		font-weight: bold;
	}

	.mymoney view:nth-child(3) {
		align-self: center;
		height: 80rpx;
		line-height: 80rpx;
		font-size: 0.9rem;
		display: flex;
		justify-content: center;
		margin: 0 auto;
		border-radius: 0rpx;

		margin-top: 70rpx;
		background: #ff9003;
		width: 60%;
		color: #fff;
		border-radius: 5rpx;
		font-size: 1rem;
	}


	.noteoper {
		width: 700rpx;
		background: #fff;
		height: 200rpx;
		border-radius: 25rpx;
		margin-top: -80rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.noteoper .item_note {
		display: flex;
		flex-direction: row;
		width: 350rpx;
		justify-content: center;
	}

	.noteoper .item_note view:nth-child(1) {
		margin-right: 8rpx;
	}

	.item_note image {
		width: 80rpx;
		height: 80rpx;
	}

	.item_title {
		display: flex;
		flex-direction: column;
	}

	.item_title view:nth-child(1) {
		font-size: 14px;
		font-weight: bold;
	}

	.item_title view:nth-child(2) {
		font-size: 12px;
		color: #a2a2a2;
	}

	.navlist {
		width: 700rpx;
		display: flex;
		justify-content: space-around;
		flex-direction: row;
		padding-top: 20rpx;
		flex-wrap: wrap;
		background: #fff;
		border-radius: 25rpx;
		margin-top: -80rpx;
		height: 240rpx;
		align-items: center;
	}

	.navlist .item {
		width: 130rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 0.7rem;
		padding-bottom: 30rpx;
		position: relative;
	}

	.tipmsg {
		background: red;
		color: #fff;
		width: 35rpx;
		height: 35rpx;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		/*
  padding:8rpx;
  border-radius: 100%;
  */
		position: absolute;
		top: -12rpx;
		left: 80rpx;
		z-index: 1000;

		font-size: 0.7rem;
	}

	.tipmsg {
		background: #ff6b6b;
		color: #fff;
		width: 90rpx;
		height: 50rpx;
		border-radius: 15rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		/*
  padding:8rpx;
  border-radius: 100%;
  */
		position: absolute;
		top: -12rpx;
		left: 80rpx;
		z-index: 1000;

		font-size: 0.7rem;
	}

	.navlist .item image {
		width: 80rpx;
		height: 80rpx;
	}

	.container .list {
		width: 700rpx;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
		background: #fff;
		border-radius: 25rpx;
	}

	.container .list .item {
		height: 80rpx;
		font-size: 14px;
		border-bottom: 0rpx solid #efefef;
		padding-left: 20rpx;
		color: #747474;
		display: flex;
		align-items: center;
		flex-direction: row;
	}

	.container .list .item image {
		width: 55rpx;
		height: 55rpx;
		margin-right: 10rpx;
	}
</style>